package com.snoopy.gas1.service.impl;

import com.snoopy.gas1.entity.Employee_info;
import com.snoopy.gas1.entity.Register;
import com.snoopy.gas1.mapper.Employee_infoMapper;
import com.snoopy.gas1.mapper.RegisterMapper;
import com.snoopy.gas1.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

// 事务注解：确保两个表同时插入成功或失败
@Service
@Transactional
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private Employee_infoMapper employeeInfoMapper;

    @Autowired
    private RegisterMapper registerMapper;

    @Override
    public void register(Employee_info employee, Register register) {
        // 1. 先插入employee_info表，获取自增id
        employeeInfoMapper.insert(employee);
        // 2. 设置关联的employee_id（MyBatis会自动回填自增id到employee对象）
        register.setEmployeeId(employee.getId());
        // 3. 插入register表
        registerMapper.insert(register);
    }
}